﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yule8xCommon.Applib;
using Yule8xCommon.Db;
using Yule8xCommon.Typelib;
using Yule8xModel;
using Yule8xWechat.Applib;

namespace Yule8xWechat.Withdraw
{
    public partial class Profile : System.Web.UI.Page
    {
        protected WxUserInfoResult wxUser;
        protected string alipay_account = "";
        protected string bank_account = "";
        protected string bank_name = "";
        protected string bank_holder = "";
        protected int record_id,user_id;
        protected void Page_Load(object sender, EventArgs e)
        {
            user_id = RequestUtil.GetInteger("user_id");
            if (RuntimeDataUtil.CheckDebugModel() && user_id > 0)
            {
                wxUser = UserModels.GetWxUserInfo(user_id);
                if (null == wxUser)
                {
                    ResponseUtil.WriteEnd("user_id无效!");
                }
            }
            else
            {
                wxUser = LoginPageBase.GetLoginInfo();
                if (null == wxUser)
                {
                    Response.Redirect(WechatUrls.GetUserInfoUrl("withdraw"), true);
                }
            }
            /*
            object loginData = Session["login"];
            if (null != loginData)
            {
                wxUser = loginData as WxUserInfoResult;
            }
            if (null == loginData || null == wxUser)
            {
                Response.Redirect(WechatUrls.GetUserInfoUrl("withdraw"), true);
            }
            */

            string action = RequestUtil.FormString("action");
            if ("submit" == action)
            {
                this.DoSubmit();
            }
            else
            {
                string sqlstmt = string.Format("select top 1 id, alipay_account, bank_account, bank_name, bank_holder from t_bankcard where user_id={0}", wxUser.user_id);
                DataTable dtBankcard = DbFactory.Init(AppInfo.conn_yule8x_agent).ExecuteDataTable(sqlstmt);
                if (dtBankcard.Rows.Count > 0)
                {
                    record_id = DataTableUtil.GetInteger(dtBankcard, "id");
                    alipay_account = DataTableUtil.GetString(dtBankcard, "alipay_account");
                    bank_account = DataTableUtil.GetString(dtBankcard, "bank_account");
                    bank_name = DataTableUtil.GetString(dtBankcard, "bank_name");
                    bank_holder = DataTableUtil.GetString(dtBankcard, "bank_holder");
                }
            }
        }
        void DoSubmit()
        {
            alipay_account = RequestUtil.FormString("alipay_account").Trim();
            bank_account = RequestUtil.FormString("bank_account").Trim();
            bank_name = RequestUtil.FormString("bank_name");
            bank_holder = RequestUtil.FormString("bank_holder");
            string sqlstmt, title;
            int uid = wxUser.user_id;

            sqlstmt = string.Format("select count(0) from t_bankcard where user_id=" + uid);
            int count = DbFactory.Init(AppInfo.conn_yule8x_agent).ExecuteCount(sqlstmt);
            title = string.Format("{0}***{1}", bank_name, (bank_account.Length > 9 ? bank_account.Substring(bank_account.Length - 3) : bank_account));
            Dictionary<string, string> data = new Dictionary<string, string>();
            data.Add("bank_account", bank_account);
            data.Add("bank_name", bank_name);
            data.Add("bank_holder", bank_holder);
            data.Add("title", title);
            data.Add("user_id", uid.ToString());
            if (count > 0)
            {
                sqlstmt = DbFactory.GetUpdateStatement("t_bankcard", "user_id=" + uid, data);
            }
            else
            {
                sqlstmt = DbFactory.GetInsertStatement("t_bankcard", data);
            }
            //title = string.Format("支付宝{0}**", (alipay_account.Length > 6 ? alipay_account.Substring(0, 6) : alipay_account));
            DbFactory.Init(AppInfo.conn_yule8x_agent).ExecuteNonQuery(sqlstmt);
            Response.Redirect("Default.aspx", true);
        }
    }
}